/*
 * Copyright 2011 Michael Guo
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package edu.uc.cs.guome.ir;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

/**
 * Generate the inverted index
 * @author(name="Michael Guo",date="01/18/2011")
 * @version 0.1
 */
public class IndexGenerator {
	
	public InvertedIndex generate() {
		
		InvertedIndex invertedIndex = new InvertedIndex();
		int isize = 0;
		int dsize = 0;
		System.out.println("Start generating Index");
		long start = System.nanoTime(); 
		for (int i=201001;i<=201012;i++) {
			ArrayList<String> files = new ArrayList<String>();
			files.add("c://ir//"+i+".txt");
			String swfile = "c://ir//stops.txt";
			
			StringBuffer sb = new StringBuffer();
			sb.append("\tprocessing c://ir//"+i+".txt; ");
			DocumentGenerator dg = new DocumentGenerator(files, swfile);
			
			isize = invertedIndex.getInvertedIndex().size();
			dsize = invertedIndex.getDocumentSet().size();
			
			try {
				HashMap<Integer, Document> docs = dg.generate();
				Iterator<Integer> k = docs.keySet().iterator();
				while(k.hasNext()) {
					Document d = docs.get(k.next());
					invertedIndex.add(d);
				}	
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			isize = invertedIndex.getInvertedIndex().size() - isize;
			dsize = invertedIndex.getDocumentSet().size() - dsize;
			
			sb.append(dsize + " document objects are added; " + isize + " new terms are indexed");
			
			System.out.println(sb.toString());
		}
		
		System.out.println("\tall input files are processed");
		System.out.println("\tIndex is generated!");
		isize = invertedIndex.getInvertedIndex().size();
		dsize = invertedIndex.getDocumentSet().size();
		double elapsedTimeInSec = (System.nanoTime() - start) * 1.0e-9;
		System.out.println("Totally " + dsize + " document objects are created, " + isize + " terms are indexed; " + elapsedTimeInSec + " seconds are used");
		System.out.println("Saving index to disk...");
		invertedIndex.saveInvertedIndex("c://ir//index.txt");
		System.out.println("Index is saved to c://ir//index.txt");
		return invertedIndex;
	}
	

}
